//
//  FMMoreAnchorViewController.m
//  PsychologyFM
//
//  Created by 杜建虎 on 16/1/13.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import "FMMoreAnchorViewController.h"
#import "FMAntorMoreModel.h"
#import "PKRequestManager.h"
#import "FMAntorOtherTableViewCell.h"
#import "MJRefresh.h"
#import "FMAntorDescViewController.h"

@interface FMMoreAnchorViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign)NSInteger requestStart; // 刷新请求的个数
@property (nonatomic, strong)UILabel *titleLabel; // 刷新请求时的提示label
@property (nonatomic, assign)BOOL isRefreshing; // 记录是否刷新
@end

@implementation FMMoreAnchorViewController


- (void)viewDidLoad {
    _dataArr = [NSMutableArray array];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _requestStart = 0;  // 初值设置为0
    _isRefreshing = NO; // 初值设置为NO(不是刷新)
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, - 40, kScreenWidth, 40)];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor grayColor];        // 设置刷新提示视图
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self addrequestData];
    [self addTableView];
    [self tableBarItem];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _requestStart += 10;
        [self addrequestData];
    }];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        _requestStart += 10;
//        [self addrequestData];;
//    }];
}

- (void)addrequestData
{
    [PKRequestManager requestWithUrlString:[NSString stringWithFormat:@"http://yiapi.xinli001.com/fm/diantai-find-list.json?key=c0d28ec0954084b4426223366293d190&rows=10&offset=%ld",_requestStart] parDic:nil method:GET finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = dic[@"data"];
        if (_requestStart == 0) {
            [_dataArr removeAllObjects];
        }
        for (NSDictionary *Dic in array) {
            FMAntorMoreModel *model = [[FMAntorMoreModel alloc] init];
            [model setValuesForKeysWithDictionary:Dic];
            [_dataArr addObject:model];
        }
        [_tableView reloadData];

    } error:^(NSError *error) {
        
    }];
}

- (void)addTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 75;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[FMAntorOtherTableViewCell class] forCellReuseIdentifier:@"FMAntorOtherTableViewCell"];
}

- (void)tableBarItem
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"く" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftItem:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
//    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [backBtn setFrame:CGRectMake(10, 10, 25, 25)];
//    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
//    [backBtn setBackgroundImage:[UIImage imageNamed:@"fanhui2"] forState:UIControlStateNormal];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
//    self.navigationItem.leftBarButtonItem = backItem;
    
}



#pragma --- 其他点击方法
- (void)leftItem:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma --- tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FMAntorOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FMAntorOtherTableViewCell"];
    FMAntorMoreModel *model = _dataArr[indexPath.row];
    cell.model = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FMAntorDescViewController *AnDescVC = [[FMAntorDescViewController alloc] init];
    FMAntorMoreModel *model = _dataArr[indexPath.row];
    AnDescVC.idStr = model.ID;
    [self.navigationController pushViewController:AnDescVC animated:YES];
}

#pragma --- self.view 生命周期方法
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}

// 点击跳转之前,让scrollView不允许滑动
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    _rootVC.scrollView.scrollEnabled = NO;
    _rootVC.tabbar.hidden = YES;
}




#pragma --- 刷新视图的方法

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    // 取得tableView的Y 偏移量
//    CGFloat y = scrollView.contentOffset.y;
//    // 被拖拽时不刷新,松手才刷新 只改变提示
//    if (scrollView.isDragging) {
//        if (y < 0 && y > -40) {
//            _titleLabel.text = @"即将刷新";
//        }
//        if (y <= -40) {
//            _titleLabel.text = @"松手刷新";
//        }
//    }
//    else
//    {
//        if (y < -40) {
//            if (_isRefreshing) {
//                return;
//            }
//            _titleLabel.text = @"正在刷新";
//            [scrollView setContentInset:UIEdgeInsetsMake(40, 0, 0, 0)];
//            _requestStart = 0;
//            [self addrequestData];
//        }
//    }
//    
//}

@end
